package com.ymatou.authapp.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.authapp.parameter.LockIpRequestDtoBean;
import com.ymatou.authapp.service.LockIpRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.mongo.YmatouBlacklistUserWapper;
import com.ymttest.utils.constant.ECode;
/**
*锁定Ip接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_LockIpRequestDto{
	private static LockIpRequestDtoBean lockiprequestdtoBean;
	private static LockIpRequestDtoCall lockiprequestdtoCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("锁定Ip接口");
	}

	@Before
	public void caseUp() {
		lockiprequestdtoBean=new LockIpRequestDtoBean();
		lockiprequestdtoCall=new LockIpRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(lockiprequestdtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LockIpRequestDto_001() {
		Logger.start(true,"锁定单个IP,验证IP被锁定");
		try {
			lockiprequestdtoBean.setIp(new String[]{"172.12.1.120"});
			lockiprequestdtoBean.setOperation("lock IP");
			lockiprequestdtoBean.setDescription("锁住Ip：172.12.1.120");
			lockiprequestdtoBean.setLockdTimeOut("PT3S");
			lockiprequestdtoCall.setData(lockiprequestdtoBean);
			lockiprequestdtoCall.callService();			
			Logger.verifyEquals(true, lockiprequestdtoCall.getSuccess(), "验证是否成功");
			Logger.verifyEquals("ok", lockiprequestdtoCall.getResult(), "验证结果信息");
			
			Logger.comment("验证IP黑名单里是否有此记录");
			YmatouBlacklistUserWapper ipLockStatusRequestDtoMongoWapper = new YmatouBlacklistUserWapper();
			boolean ipLocked = ipLockStatusRequestDtoMongoWapper.isIPLocked("172.12.1.120");
			Logger.verifyEquals(true, ipLocked, "验证IP已被锁定");
			
			Logger.comment("从黑名单中删除此Ip，恢复测试数据");
			Logger.verifyEquals(true, ipLockStatusRequestDtoMongoWapper.deleteData("172.12.1.120"), "删除成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LockIpRequestDto_002() {
		Logger.start(true,"锁定多个IP,验证IP被锁定");
		try {
			String[] ips = new String[11];
			for(int i =0; i<=10; i++){
				ips[i]="172.12.1.12"+i;
			}
			lockiprequestdtoBean.setIp(ips);
			lockiprequestdtoBean.setOperation("lock IP");
			lockiprequestdtoBean.setDescription("锁住Ip：172.12.1.120...172.12.1.1210");
			lockiprequestdtoBean.setLockdTimeOut("PT100S");
			lockiprequestdtoCall.setData(lockiprequestdtoBean);
			lockiprequestdtoCall.callService();			
			Logger.verifyEquals(true, lockiprequestdtoCall.getSuccess(), "验证是否成功");
			Logger.verifyEquals("ok", lockiprequestdtoCall.getResult(), "验证结果信息");
			
			Logger.comment("验证IP黑名单里是否有此记录");
			YmatouBlacklistUserWapper ipLockStatusRequestDtoMongoWapper = new YmatouBlacklistUserWapper();
			for(int i=0; i<=10; i++){
				boolean ipLocked = ipLockStatusRequestDtoMongoWapper.isIPLocked("172.12.1.12"+i);
				Logger.verifyEquals(true, ipLocked, "验证IP-172.12.1.12"+i+"已被锁定");
			}
			
			Logger.comment("从黑名单中删除此Ip，恢复测试数据");
			for(int i=0; i<=10; i++){
				Logger.verifyEquals(true, ipLockStatusRequestDtoMongoWapper.deleteData("172.12.1.12"+i), "删除Ip-172.12.1.12"+i+"成功");		
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_LockIpRequestDto_003() {
		Logger.start(false,"不输入Ip，失败");
		try {
			lockiprequestdtoBean.setOperation("lock IP");
			lockiprequestdtoBean.setDescription("锁住Ip为空");
			lockiprequestdtoBean.setLockdTimeOut("PT3S");
			lockiprequestdtoCall.setData(lockiprequestdtoBean);
			lockiprequestdtoCall.callService();			
			Logger.verifyEquals(false, lockiprequestdtoCall.getSuccess(), "验证是否成功");
			Logger.verifyEquals("fail", lockiprequestdtoCall.getResult(), "验证结果信息");
			Logger.verifyEquals(ECode.AUTH_NOIP2, lockiprequestdtoCall.getMsg(), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_LockIpRequestDto_004() {
		Logger.start(false,"操作为空，失败");
		try {
			lockiprequestdtoBean.setIp(new String[]{"172.12.1.120"});
			lockiprequestdtoBean.setDescription("锁住Ip: 172.12.1.120");
			lockiprequestdtoBean.setLockdTimeOut("PT3S");
			lockiprequestdtoCall.setData(lockiprequestdtoBean);
			lockiprequestdtoCall.callService();			
			Logger.verifyEquals(false, lockiprequestdtoCall.getSuccess(), "验证是否成功");
			Logger.verifyEquals("fail", lockiprequestdtoCall.getResult(), "验证结果信息");
			Logger.verifyEquals(ECode.AUTH_NOOPERATION_DESCRIPTION, lockiprequestdtoCall.getMsg(), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_LockIpRequestDto_005() {
		Logger.start(false,"操作说明为空，失败");
		try {
			lockiprequestdtoBean.setIp(new String[]{"172.12.1.120"});
			lockiprequestdtoBean.setDescription("锁住Ip: 172.12.1.120");
			lockiprequestdtoBean.setLockdTimeOut("PT3S");
			lockiprequestdtoCall.setData(lockiprequestdtoBean);
			lockiprequestdtoCall.callService();			
			Logger.verifyEquals(false, lockiprequestdtoCall.getSuccess(), "验证是否成功");
			Logger.verifyEquals("fail", lockiprequestdtoCall.getResult(), "验证结果信息");
			Logger.verifyEquals(ECode.AUTH_NOOPERATION_DESCRIPTION, lockiprequestdtoCall.getMsg(), "验证Msg信息");		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}